//
//  AddressAnnotationView.m
//  flutter_plugin_gdmap
//
//  Created by YunXing on 2021/11/4.
//
#import "AddressAnnotationView.h"
#import "AddressPointAnnotation.h"
#import "CommonUtility.h"

@interface AddressAnnotationView ()

@property (nonatomic, strong) UILabel *sortLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *addressLabel;

@end

@implementation AddressAnnotationView

- (void)drawRect:(CGRect)rect
{
    [self drawInContext:UIGraphicsGetCurrentContext()];
    //阴影
    self.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:.1f].CGColor;
    self.layer.shadowOpacity = .8f;
    self.layer.shadowRadius = 4.f;
    self.layer.shadowOffset = CGSizeMake(0.0f, 2.f);
}

- (void)drawInContext:(CGContextRef)context
{
    //弹窗背景
    CGContextSetLineWidth(context, 1.0);
    CGContextSetFillColorWithColor(context, [[CommonUtility colorWithHexString:@"#F6744D"] CGColor]);
    [self getDrawPath:context];
    CGContextFillPath(context);
}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 8.0;//弹窗圆角弧度
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-10;
    
    CGContextMoveToPoint(context, midx+10, maxy);
    CGContextAddLineToPoint(context,midx, maxy+10);
    CGContextAddLineToPoint(context,midx-10, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, 200, 68);
        self.backgroundColor = [UIColor clearColor];
        
        // 添加排序文本
            self.sortLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 38, 38)];
            self.sortLabel.textAlignment = NSTextAlignmentCenter;
            self.sortLabel.backgroundColor = [CommonUtility colorWithHexString:@"#FFDDC1"];
            self.sortLabel.layer.cornerRadius = 20.f;
            self.sortLabel.layer.masksToBounds = YES;
            self.sortLabel.font = [UIFont boldSystemFontOfSize:14];
            self.sortLabel.textColor = [UIColor blackColor];
            [self addSubview:self.sortLabel];
            
            // 添加手机号文本
            self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_sortLabel.frame) + 6, 10, CGRectGetWidth(self.frame) - CGRectGetMaxX(_sortLabel.frame) - 20, 18)];
            self.phoneLabel.font = [UIFont boldSystemFontOfSize:13];
            self.phoneLabel.textColor = [UIColor whiteColor];
            [self addSubview:self.phoneLabel];
            
            // 添加地址文本
            self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_sortLabel.frame) + 6, 30, CGRectGetWidth(self.frame) - CGRectGetMaxX(_sortLabel.frame) - 20, 18)];
            self.addressLabel.font = [UIFont boldSystemFontOfSize:13];
            self.addressLabel.textColor = [UIColor whiteColor];
            self.addressLabel.numberOfLines=1;
            [self addSubview:self.addressLabel];
    }
    
    return self;
}

- (void)setNumSort:(NSString *)numSort
{
    self.sortLabel.text = numSort;
}

- (void)setPhone:(NSString *)phone setTicketNumber:(NSInteger)ticketNumber
{
    NSString *phoneLast = [phone substringFromIndex:phone.length - 4];
    self.phoneLabel.text = [NSString stringWithFormat:@"尾号%@ / %ld人", phoneLast, ticketNumber];
}

- (void)setAddress:(NSString *)address
{
    self.addressLabel.text = address;
}

@end
